﻿using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

public class MsgCenter:Sington<MsgCenter>
{

 


    public delegate void MsgDel(EventArgs e);
    public delegate void MsgDelNoPram();


    Dictionary<string, MsgDel> DicMsg = new Dictionary<string, MsgDel>();
    Dictionary<string, MsgDelNoPram> DicNoPram = new Dictionary<string, MsgDelNoPram>();

    /// <summary>
    /// 注册消息，记得要在物体销毁前取消注册
    /// </summary>
    /// <param name="name"></param>
    /// <param name="a"></param>
    public void RegisterMsgWithPram(string name, MsgDel a)
    {
        if (!DicMsg.ContainsKey(name))
        {
            DicMsg.Add(name, a);
        }
        else
            DicMsg[name] += a;
    }

    public void RegisterMsg(string name, MsgDelNoPram a)
    {
        if (!DicNoPram.ContainsKey(name))
        {
            DicNoPram.Add(name, a);
        }
        else
            DicNoPram[name] += a;
    }
    /// <summary>
    /// 取消注册
    /// </summary>
    /// <param name="name"></param>
    /// <param name="a"></param>
    public void UnRegisterMsg(string name, MsgDel a)
    {
        if (DicMsg.ContainsKey(name))
        {
            DicMsg[name] -= a;
        }
    }
    /// <summary>
    /// 取消注册
    /// </summary>
    /// <param name="name"></param>
    /// <param name="a"></param>
    public void UnRegisterMsg(string name, MsgDelNoPram a)
    {
        if (DicNoPram.ContainsKey(name))
        {
            DicNoPram[name] -= a;
        }
    }
    /// <summary>
    /// 发送消息
    /// </summary>
    /// <param name="name"></param>
    /// <param name="e"></param>
    public void SendMsg(string name, EventArgs e = null)
    {
        if (DicMsg.ContainsKey(name))
        {
            if (DicMsg[name] != null)
            {
                DicMsg[name](e);
            }
        }
        if (DicNoPram.ContainsKey(name))
        {
            if (DicNoPram[name] != null)
            {
                DicNoPram[name]();
            }
        }
    }
    public void Clear(string name)
    {
        if (DicNoPram.ContainsKey(name))
        {
            DicNoPram.Remove(name);
        }
        if (DicMsg.ContainsKey(name))
        {
            DicMsg.Remove(name);
        }
    }
    public void ClearAll()
    {
        DicMsg.Clear();
        DicNoPram.Clear();
    }

}
public class EventArgs
{
    public EventArgs() { }
    public EventArgs(int i)
    {
        ParamInt = i;
    }
    public EventArgs(float i)
    {
        ParamFloat = i;
    }
    public EventArgs(string i)
    {
        ParamString = i;
    }
    public EventArgs(bool i)
    {
        ParamBool = i;
    }
    public int ParamInt;
    public string ParamString;
    public float ParamFloat;
    public bool ParamBool;
}
/// <summary>
/// 实现Clear方法，清除该类所有的消息
/// </summary>
public   class MsgBase<T>
{

    public static T var;
   
    public static FieldInfo[] GetConstants(System.Type type)
    {
        ArrayList constants = new ArrayList();

        FieldInfo[] fieldInfos = type.GetFields(
            BindingFlags.Public | BindingFlags.Static |
            BindingFlags.FlattenHierarchy);

        foreach (FieldInfo fi in fieldInfos)
            if (fi.IsLiteral && !fi.IsInitOnly)
                constants.Add(fi);

        return (FieldInfo[])constants.ToArray(typeof(FieldInfo));
    }
  
    public static void ClearMsg()
    {
        
        foreach (var info in GetConstants(typeof(T)))
        {
            MsgCenter.Instance.Clear(info.Name);

        }
    }

}
public  class JZXMSG:MsgBase<JZXMSG>
{
 
    public const string GameLose = "GameLost";
    public const string GameWin = "GameWin";
    public const string GuliReturn = "GuliReturn";
    public const string Reset = "Reset";
}
public class GameMSG 
{
    public const string OnNewObject = "OnNewObject";
}